package com.sunchangpeng.wechat.mp.media;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import com.sunchangpeng.wechat.mp.media.bean.*;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.sunchangpeng.wechat.core.utils.StringPool.MEDIA_NAME;

public class Materials extends Mpnews {
    public static Materials with(MpAppSetting appSetting) {
        Materials materials = new Materials();
        materials.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return materials;
    }

    /**
     * 新增图片（image）类型永久素材
     */
    public Material uploadImage(String fileName, byte[] content) {
        return upload(MediaType.image, fileName, content);
    }

    /**
     * 获取图片（image）类型永久素材
     */
    public File downloadImage(String mediaId) {
        return download(mediaId);
    }

    /**
     * 图片（image）类型永久素材列表
     */
    public MaterialPage listImage(int offset, int count) {
        return list(MediaType.image, offset, count);
    }

    /**
     * 新增语音（voice）类型永久素材
     */
    public Material uploadVoice(String fileName, byte[] content) {
        return upload(MediaType.image, fileName, content);
    }

    /**
     * 获取语音（voice）类型永久素材
     */
    public File downloadVoice(String mediaId) {
        return download(mediaId);
    }

    /**
     * 语音（voice）类型永久素材列表
     */
    public MaterialPage listVoice(int offset, int count) {
        return list(MediaType.voice, offset, count);
    }

    /**
     * 新增缩略图（thumb）类型永久素材
     */
    public Material uploadThumb(String fileName, byte[] content) {
        return upload(MediaType.image, fileName, content);
    }

    /**
     * 获取缩略图（thumb）类型永久素材
     */
    public File downloadThumb(String mediaId) {
        return download(mediaId);
    }

    private Material upload(MediaType type, String fileName, byte[] content) {
        String url = EndpointProvider.get("url.material.binary.upload");
        String response = wechatClient.multiFormWithToken(String.format(url, type.name()), MEDIA_NAME, fileName, content);
        return JSON.parseObject(response, Material.class);
    }

    private File download(String mediaId) {
        return wechatClient.getTempFileWithToken(EndpointProvider.get("url.material.binary.get"), JSON.toJSONString(new JSONObject().fluentPut("media_id", mediaId)));
    }

    private MaterialPage list(MediaType mediaType, int offset, int count) {
        String url = EndpointProvider.get("url.material.list");
        String response = wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject()
                .fluentPut("type", mediaType.name())
                .fluentPut("offset", offset)
                .fluentPut("count", count)));
        return JSON.parseObject(response, MaterialPage.class);
    }

    /**
     * 新增视频（video）类型永久素材
     */
    public Material uploadVideo(String fileName, byte[] content, String title, String introduction) {
        String url = EndpointProvider.get("url.material.binary.upload");

        JSONObject description = new JSONObject().fluentPut("title", title).fluentPut("introduction", introduction);
        Map<String, String> form = new HashMap<>();
        form.put("description", JSON.toJSONString(description));

        String response = wechatClient.multiFormWithToken(String.format(url, MediaType.video.name()), MEDIA_NAME, fileName, content, form);
        return JSON.parseObject(response, Material.class);
    }

    /**
     * 获取视频（video）类型永久素材
     */
    public Video getVideo(String mediaId) {
        String url = EndpointProvider.get("url.material.binary.get");
        String response = wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject().fluentPut("media_id", mediaId)));
        return JSON.parseObject(response, Video.class);
    }

    /**
     * 视频（video）类型永久素材列表
     */
    public MaterialPage listVideo(int offset, int count) {
        return list(MediaType.video, offset, count);
    }

    /**
     * 删除永久素材
     */
    public void delete(String mediaId) {
        String url = EndpointProvider.get("url.material.delete");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject().fluentPut("media_id", mediaId)));
    }

    /**
     * 获取素材总数
     */
    public Counts count() {
        String response = wechatClient.getTextWithToken(EndpointProvider.get("url.material.count"));
        return JSON.parseObject(response, Counts.class);
    }
}
